/* 换二维码 */ 
var requestManager = require('../../../utils/requestManager.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    posterUrl: '',
    qrcode: '',
    qrcodeUrl: '',
    qrcodeList: []
  },

  /////////////// 网络请求 ///////////////
  /**
   * 上传图片到七牛
   */
  requestUpImage: function (localImageUrl) {
    let that=this;
    wx.showLoading({
      title: '图片上传中...',
    })
    requestManager.uploadimg({
      path:[localImageUrl],
      finished: function (uploadRes) {
        wx.hideLoading()
        console.log(uploadRes)
        //拼接域名 + 图片名字
      
        wx.setStorageSync('qrcodeUrl', uploadRes.images[0])
        wx.setStorageSync('qrcode', uploadRes.imageNames[0])

        if (!uploadRes.images.length){
          wx.setStorageSync('qrcodeUrl', that.data.qrcodeUrl)
          wx.setStorageSync('qrcode', that.data.qrcode)
          
        }
        // 返回上一页
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },

  /////////////// 网络请求 ///////////////

  /* 选择二维码 */ 
  selectQrCodeTap: function (event) {
    let currentSelectIndex = event.currentTarget.dataset.index;
    var array = this.data.qrcodeList;
    array.forEach((item, index, arr) => {
      let select = item.selected;
      if (select && index == currentSelectIndex) {
        // 如果是选的同一张图 取消操作
        var sItem = "qrcodeList[" + index + "].selected";
        this.setData({
          [sItem]: false,
          qrcode: '',
          qrcodeUrl: ''
        })
      } else {
        if (index == currentSelectIndex) {
          var onSelected = "qrcodeList[" + index + "].selected";
          this.setData({
            [onSelected]: true,
            qrcode: item.image,
            qrcodeUrl: item.imageUrl
          })
        } else {
          var sItem = "qrcodeList[" + index + "].selected";
          this.setData({
            [sItem]: false
          })
        }
      }
    });
  },
  /* 添加本地二维码 */
  addQrCodeTap: function (event) {
    var that = this;
    // 跳到相册选取图片
    wx.chooseImage({
      count: 1,
      // sizeType: "compressed",//压缩
      // sourceType: "album",//相册
      success: function (res) {
        console.log(res)
        let tmepFilePathList = res.tempFilePaths;
        var qrCodeArray = that.data.qrcodeList;
        let lastIndex = qrCodeArray.length;
        tmepFilePathList.forEach((item, index, arr) => {
          // 创建二维码对象
          let qrCodeItem = {
            'image': '',
            'imageUrl': item,
            'selected': false
          };
          // 添加到数组里面
          qrCodeArray.push(qrCodeItem);
        });
        // 更新二维码数据
        that.setData({
          qrcodeList: qrCodeArray
        })
      },
      fail:function(err){
        console.log(err)
      },
      complete:function(obj){
        console.log(obj)
      }
    })
  },

  /* 完成按钮 tap事件 */
  completeTap: function (event) {
    // 找到要上传的图片
    let qrcodeArray = this.data.qrcodeList
    let tQrcode = this.data.qrcode
    let tQrcodeUrl = this.data.qrcodeUrl
    console.log('tQrcodeUrl' + tQrcodeUrl)
    if (tQrcodeUrl.length < 1) {
      console.log('请先选择图片')
      return;
    }

    if (tQrcodeUrl != wx.getStorageSync('qrcodeUrl')) {
      // 本地图片 走上传图片代码
      this.requestUpImage(tQrcodeUrl)
    }else {
      // 保存选择的二维码
      wx.navigateBack({
        delta: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()//隐藏转发按钮
    let that = this;
    let shop_id = requestManager.shopParams().shop_id
    //请求店铺二维码
    requestManager.postRequest("xcx/create/qrcode",{
      page:"pages/index/index",
      width:"100",
      shop:shop_id,
      type:3,
      id:""
    },function(res){

      var qrCodeArray = that.data.qrcodeList;
      let tPosterUrl = wx.getStorageSync("posterUrl")

      // 加载海报及二维码
      // let tPosterUrl = wx.getStorageSync("posterUrl")
      let tQrCode = wx.getStorageSync("qrcode")
      let tQrCodeUrl = wx.getStorageSync("qrcodeUrl")
      if(tQrCode&&tQrCode.length > 0) {
        // 创建二维码对象
        let qrCodeItem = {
          'image': tQrCode,
          'imageUrl': tQrCodeUrl,
          'selected': true
        };
        // 添加到数组里面
        qrCodeArray.push(qrCodeItem);
      }
      // 更新数据及相关UI
      that.setData({
        posterUrl: tPosterUrl,
        qrcode: tQrCode,
        qrcodeUrl: tQrCodeUrl,
        qrcodeList: qrCodeArray
      })
      if (res.detail.path == wx.getStorageSync("qrcodeUrl")) {
        return
      }
      console.log(res)

      let shopQrcode = {
        'image': res.detail.path,
        'imageUrl': res.detail.path,
        'selected': false
      }
      qrCodeArray.push(shopQrcode);
      that.setData({
        qrcodeList: qrCodeArray
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})